/**
 * 修改或添加意愿
 */
package org.morningangel.servlet.android;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.morningangel.android.bean.WillBeanList;
import org.morningangel.bean.Will;
import org.morningangel.dao.WillDao;
import org.morningangel.dao.impl.WillDaoImpl;

@WebServlet(name = "AndroidUpdateWillServlet", urlPatterns = "/android/updateWill")
public class UpdateWillServlet extends HttpServlet {
	private static final long serialVersionUID = -4676301825984772083L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
	    Integer userId  = Integer.parseInt(request.getParameter("userId"));
		Integer willId = Integer.parseInt(request.getParameter("willId"));
		String userName = request.getParameter("userName");
		String telNum = request.getParameter("telNum");
		String area = request.getParameter("area");
		String type = request.getParameter("type");
		String ideaTime = request.getParameter("ideaTime");
		String willContent = request.getParameter("willContent");
		WillDao willDao = new WillDaoImpl();
		Will will = new Will();
		will.setUserId(userId);
		will.setId(willId);
		will.setUserName(userName);
		will.setTelNum(telNum);
		will.setArea(area);
		will.setType(type);
		will.setIdeaTime(ideaTime);
		will.setWillContent(willContent);
		will.setTime(new Date());
		if (willId == 0) {
			willDao.save(will);
		} else {
			willDao.update(will);
		}
		List<Will> list = willDao.getByUserId(userId);
		WillBeanList result = new WillBeanList(list);
		JSONObject jsonObject = JSONObject.fromObject(result);
		PrintWriter out = response.getWriter();
		out.print(jsonObject);
		out.flush();
		out.close();
	}

}
